package com.deyuanyun.pic.ctrl.service.impl;

import com.deyuanyun.pic.common.tool.JXL;
import com.deyuanyun.pic.common.util.DateUtil;
import com.deyuanyun.pic.common.util.StringUtils;
import com.deyuanyun.pic.common.util.poi.ExcelUtil;
import com.deyuanyun.pic.common.util.web.WebUtil;
import com.deyuanyun.pic.ctrl.controller.vo.query.EqptQueryVO;
import com.deyuanyun.pic.ctrl.controller.vo.*;
import com.deyuanyun.pic.ctrl.enumeration.EqptTypeEnum;
import com.deyuanyun.pic.ctrl.mapper.ExternalCrsnEqptMapper;
import com.deyuanyun.pic.ctrl.service.ExternalCrsnEqptService;
import com.deyuanyun.pic.settings.controller.pipe.vo.PipeLocationDTO;
import com.deyuanyun.pic.settings.domain.util.DomainCodeInterceptor;
import com.deyuanyun.pic.settings.service.pipe.PipeLocationService;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.IOException;
import java.math.BigDecimal;
import java.text.ParseException;
import java.util.Date;
import java.util.List;

/**
 * 
 * @author lijun
 * @date 2016-09-06
 * 外腐蚀设备管理业务层
 * History: [修改人][时间][修改描述]
 */
@Service
public class ExternalCrsnEqptServiceImpl implements ExternalCrsnEqptService{

    @Resource
    private ExternalCrsnEqptMapper externalCrsnEqptMapper;
    @Resource
    private PipeLocationService pipeLocationService;

    @Override
    public List<EqptCppowerVO> queryEqptCppower(EqptQueryVO eqptQuery) {

        List<EqptCppowerVO> list = externalCrsnEqptMapper.queryEqptCppower(eqptQuery);
        //转换CODE表外键ID为对应名称
        list = DomainCodeInterceptor.newProxyInstance(list);
        return list;
    }

    @Override
    public List<EqptReferenceVO> queryEqptReference(EqptQueryVO eqptQuery) {
        List<EqptReferenceVO> list = externalCrsnEqptMapper.queryEqptReference(eqptQuery);
        //转换CODE表外键ID为对应名称
        list = DomainCodeInterceptor.newProxyInstance(list);
        return list;
    }

    @Override
    public List<EqptAnodesbedVO> queryEqptAnodesbed(EqptQueryVO eqptQuery) {
        List<EqptAnodesbedVO> list =externalCrsnEqptMapper.queryEqptAnodesbed(eqptQuery);
        //转换CODE表外键ID为对应名称
        list = DomainCodeInterceptor.newProxyInstance(list);
        return list;
    }

    @Override
    public List<EqptInsulatedJointVO> queryEqptInsulatedJoint(EqptQueryVO eqptQuery) {
        List<EqptInsulatedJointVO> list = externalCrsnEqptMapper.queryEqptInsulatedJoint(eqptQuery);
        //转换CODE表外键ID为对应名称
        list = DomainCodeInterceptor.newProxyInstance(list);
        return list;
    }

    @Override
    public List<EqptAntiSurgeVO> queryEqptAntiSurge(EqptQueryVO eqptQuery) {
        List<EqptAntiSurgeVO> list = externalCrsnEqptMapper.queryEqptAntiSurge(eqptQuery);
        //转换CODE表外键ID为对应名称
        list = DomainCodeInterceptor.newProxyInstance(list);
        return list;
    }

    @Override
    public List<EqptCpJumperVO> queryEqptCpJumper(EqptQueryVO eqptQuery) {
        List<EqptCpJumperVO> list = externalCrsnEqptMapper.queryEqptCpJumper(eqptQuery);
        //转换CODE表外键ID为对应名称
        list = DomainCodeInterceptor.newProxyInstance(list);
        return list;
    }

    @Override
    public List<EqptCptestStationVO> queryEqptCptestStation(EqptQueryVO eqptQuery) {
        List<EqptCptestStationVO> list = externalCrsnEqptMapper.queryEqptCptestStation(eqptQuery);
        //转换CODE表外键ID为对应名称
        list = DomainCodeInterceptor.newProxyInstance(list);
        return list;
    }

    @Override
    public List<EqptSaalAnodeVO> queryEqptSaalAnode(EqptQueryVO eqptQuery) {
        List<EqptSaalAnodeVO> list = externalCrsnEqptMapper.queryEqptSaalAnode(eqptQuery);
        //转换CODE表外键ID为对应名称
        list = DomainCodeInterceptor.newProxyInstance(list);
        return list;
    }

    @Override
    public List<EqptCurrentDrainageVO> queryEqptCurrentDrainage(EqptQueryVO eqptQuery) {
        List<EqptCurrentDrainageVO> list = externalCrsnEqptMapper.queryEqptCurrentDrainage(eqptQuery);
        //转换CODE表外键ID为对应名称
        list = DomainCodeInterceptor.newProxyInstance(list);
        return list;
    }

    @Override
    public Workbook queryEqptDataExl(EqptQueryVO eqptQuery, EqptTypeEnum eqptType) throws IOException, ParseException {
        List eqpt = null;
        String filePath =null;
        String downName = "";
        int beginRow = 3;
        if(eqptType.equals(EqptTypeEnum.CPPOWER)){
            eqpt = this.queryEqptCppower(eqptQuery);
            filePath = "/template/excl/ctrl/eqpt_cppower.xls";
            downName ="阴保电源";
        } else if(eqptType.equals(EqptTypeEnum.ANODESBED)){
            eqpt = this.queryEqptAnodesbed(eqptQuery);
            filePath = "/template/excl/ctrl/eqpt_anodesbed.xls";
            downName ="阳极地床";
        }else if(eqptType.equals(EqptTypeEnum.ANTISURGE)){
            eqpt = this.queryEqptAntiSurge(eqptQuery);
            filePath = "/template/excl/ctrl/eqpt_antiSurge.xls";
            downName ="防浪涌保护器";
            beginRow = 4;
        }else if(eqptType.equals(EqptTypeEnum.CPJUMPER)){
            eqpt = this.queryEqptCpJumper(eqptQuery);
            filePath = "/template/excl/ctrl/eqpt_cpJumper.xls";
            downName ="跨接线";
            beginRow = 4;
        }else if(eqptType.equals(EqptTypeEnum.CPTESTSTATION)){
            eqpt = this.queryEqptCptestStation(eqptQuery);
            filePath = "/template/excl/ctrl/eqpt_cptestStation.xls";
            downName ="阴保测试桩";
        }else if(eqptType.equals(EqptTypeEnum.CURRENTDRAINAGE)){
            eqpt = this.queryEqptCurrentDrainage(eqptQuery);
            filePath = "/template/excl/ctrl/eqpt_currentDrainage.xls";
            downName ="排流装置";
            beginRow = 4;
        }else if(eqptType.equals(EqptTypeEnum.INSULATEDJOINT)){
            eqpt = this.queryEqptInsulatedJoint(eqptQuery);
            filePath = "/template/excl/ctrl/eqpt_InsulatedJoint.xls";
            downName ="绝缘装置";
            beginRow = 4;
        }else if(eqptType.equals(EqptTypeEnum.REFERENCE)){
            eqpt = this.queryEqptReference(eqptQuery);
            filePath = "/template/excl/ctrl/eqpt_reference.xls";
            downName ="长效参比";
        }else if(eqptType.equals(EqptTypeEnum.SAALANODE)){
            eqpt = this.queryEqptSaalAnode(eqptQuery);
            filePath = "/template/excl/ctrl/eqpt_saalAnode.xls";
            downName ="牺牲阳极";
            beginRow =4;
        }

        List<String[]> result = JXL.toListFormVO(eqpt, "yyyy-MM-dd");

        Workbook workbook = ExcelUtil.initWorkbook(filePath, result, beginRow - 2);//因为模板删除了头部两行，所以-2.

        String nowDate = DateUtil.DateToStringYMd(new Date());

        WebUtil.setDownHeader(downName+"设备导出表"+nowDate+".xls");

        return workbook;
    }

}
